package com.qing.thread.unsafe;

import java.util.ArrayList;
import java.util.List;

/**
 * ArrayList是线程不安全的
 */
public class UnsafeArrayList {
    public static void main(String[] args) throws InterruptedException {
        List<String> list = new ArrayList<>();
        for (int i = 0; i < 10000; i++) {
            new Thread(() -> {
                //对list会变化的对象进行同步监听
                synchronized (list) {
                    list.add(Thread.currentThread().getName());
                }
            }).start();
        }
        System.out.println(list.size());
        Thread.sleep(100);
        //list.size不满10000，说明两个线程在同一时间操作，给list同一个位置进行了赋值
        System.out.println(list.size());
    }
}
